package com.laikan.reader.bookfriend.requester;

import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.comment.APIProtocol;
import com.socks.library.KLog;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by lingyt on 2016/11/7.
 * 关注和粉丝的API
 */
public class AutherRequester {

    /**
     * 获取关注列表
     *
     * @param pageNo     页码
     * @param followerId 列表主人
     * @param listener
     */
    public static void requestAttentionList(int pageNo, String followerId, RequestListener listener) {

        String url = APIProtocol.laikan_get_attention_list;
        Map<String, String> params = new HashMap<>();
        params.put("pageNo", String.valueOf(pageNo));
        params.put("followerId", followerId);
        params.put("sd", getSd());
        KLog.w("-----------------------------获取关注列表------------------------------");
        KLog.w("请求参数：pageNo = " + pageNo + " followerId = " + followerId + " sd = " + getSd());

        HttpApiRequestEngine.getInstance().httpPostRequest(getRequester(url, params), listener);
    }


    /**
     * 添加关注
     *
     * @param objectId
     * @param listener
     */
    public static void requestAddAttention(String objectId, RequestListener listener) {

        String url = APIProtocol.laikan_add_attention;
        Map<String, String> params = new HashMap<>();
        params.put("objectId", objectId);
        params.put("sd", getSd());

        KLog.w("-----------------------------添加关注------------------------------");
        KLog.w("请求参数：objectId = " + objectId + " sd = " + getSd());

        HttpApiRequestEngine.getInstance().httpPostRequest(getRequester(url, params), listener);
    }


    /**
     * 取消关注
     *
     * @param objectId
     * @param listener
     */
    public static void requestDelAttention(String objectId, RequestListener listener) {

        String url = APIProtocol.laikan_cancel_attention;
        Map<String, String> params = new HashMap<>();
        params.put("objectId", objectId);
        params.put("sd", getSd());
        KLog.w("-----------------------------取消关注------------------------------");
        KLog.w("请求参数：objectId = " + objectId + " sd = " + getSd());
        HttpApiRequestEngine.getInstance().httpPostRequest(getRequester(url, params), listener);
    }


    /**
     * 获取粉丝列表
     *
     * @param pageNo
     * @param objectId
     * @param listener
     */
    public static void requestFunsList(int pageNo, String objectId, RequestListener listener) {

        String url = APIProtocol.laikan_get_funs_list;
        HashMap<String, String> params = new HashMap<>();

        params.put("pageNo", String.valueOf(pageNo));
        params.put("objectId", objectId);
        params.put("sd", getSd());

        KLog.w("-----------------------------获取粉丝列表------------------------------");
        KLog.w("请求参数：pageNo = " + pageNo + " objcectId = " + objectId + " sd = " + getSd());

        HttpApiRequestEngine.getInstance().httpPostRequest(getRequester(url, params), listener);
    }

    /**
     * 获取个人主页数据
     *
     * @param authorId
     * @param listener
     */
    public static void requestAutherHomeData(String authorId, int bookPageNo, int shortePageNo, RequestListener listener) {
        String url = APIProtocol.laikan_personal_home;
        HashMap<String, String> params = new HashMap<>();

        params.put("authorId", authorId);
        params.put("sd", getSd());
        params.put("bookPageNo", String.valueOf(bookPageNo));
        params.put("bookPageSize", "10");
        params.put("shortePageNo", String.valueOf(shortePageNo));
        params.put("shortePageSize", "10");

        KLog.w("-----------------------------获取个人主页数据------------------------------");
        KLog.w("请求参数：authorId = " + authorId + " shortePageNo = " + shortePageNo + " bookPageNo = " + bookPageNo + " sd = " + getSd());
        HttpApiRequestEngine.getInstance().httpPostRequest(getRequester(url, params), listener);
    }


    /**
     * 获取token
     *
     * @return
     */
    public static String getSd() {
        String token = SPUtil.getString("token", "");
        return token;
    }

    /**
     * 获取请求对象
     *
     * @param url
     * @param params
     * @return
     */
    private static Requester getRequester(String url, Map<String, String> params) {
        Requester requester = new Requester();
        requester.setUrl(url);
        requester.setMapBody(params);
        return requester;
    }

}
